package com.ledim.push;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import com.igexin.sdk.PushConsts;
import com.ledim.activity.SplashActivity;
import com.ledim.app.LeDimApplication;
import com.ledim.bean.LedimPushBean;
import com.ledim.service.coverter.LedimGsonConverterFactory;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.letv.android.young.client.R;
import java.util.Random;
import org.greenrobot.eventbus.EventBus;

/**
 * Created by ccx on 16/10/21.
 * 个推sdk receiver
 */

public class LedimPushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtil.e("ccx LedimPushReceiver");
        Bundle bundle = intent.getExtras();

        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_CLIENTID:
                LogUtil.e("ccx LedimPushReceiver cid");
                String cid = bundle.getString("clientid");
                // TODO:处理cid返回
                break;
            case PushConsts.GET_MSG_DATA:
                LedimSpUtils.setPushMgsCount(LedimSpUtils.getPushMgsCount() + 1);
                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");
                byte[] payload = bundle.getByteArray("payload");

                //// smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                //boolean result = PushManager.getInstance()
                //                            .sendFeedbackMessage(context, taskid, messageid, 90001);
                try {

                    if (payload != null) {
                        String data = new String(payload);
                        LogUtil.e("ccx LedimPushReceiver:" + data);
                        LedimPushBean bean =
                                LedimGsonConverterFactory.create().gson.fromJson(data, LedimPushBean.class);
                        if (bean != null) {
                            notificationPushMsg(bean);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void notificationPushMsg(LedimPushBean bean) {
        Intent intent = new Intent(LeDimApplication.getInstance()
                                                   .getApplicationContext(), SplashActivity.class);
        intent.putExtra(ConstantUtil.DATA, bean);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pend = PendingIntent.getActivity(LeDimApplication.getInstance()
                                                                       .getApplicationContext(), new Random()
                .nextInt(), intent, 0);
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(LeDimApplication.getInstance()
                                                               .getApplicationContext());
        builder.setContentIntent(pend)
               .setSmallIcon(R.drawable.login_icon_small, 0)
               .setWhen(System.currentTimeMillis())
               .setAutoCancel(true)
               .setContentTitle(bean.title)
               .setTicker(bean.content)//手机状态栏的提示
               .setContentText(bean.content);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 设置通知的优先级
            builder.setPriority(Notification.PRIORITY_MAX);
        } else {

        }

        NotificationManager man = (NotificationManager) LeDimApplication.getInstance()
                                                                        .getApplicationContext()
                                                                        .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = builder.getNotification();
        //boolean vibrate = PersonalConfigSP.getInstance().getVibrateSwitcher() == 1;
        //boolean sound = PersonalConfigSP.getInstance().getSoundSwitcher() == 1;
        //if (vibrate)
        //{
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        //}
        //if (sound)
        //{
        notification.defaults |= Notification.DEFAULT_SOUND;
        //}
        Message msg = new Message();
        msg.what = ConstantUtil.EventBusMsgConstant.PUSH_MSG_NOTICE;
        EventBus.getDefault().post(msg);
        man.notify(LedimSpUtils.getPushMsgId(), notification);
    }
}
